package com.zretc.controller;

import com.util.CodeMsg;
import com.zretc.pojo.User;
import com.util.Result;
import com.zretc.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


/**
 * Filename:    UserController.java
 * Description: 用户相关接口
 *              ex: 登录
 * Copyright:   Copyright (c) 2022-2025 All Rights Reserved.
 * Company:     chinasofti.com Inc.
 * @author:     mazheng
 * @version:    1.0
 * Create at:   2022-08-19
 *
 * Modification History:
 * Date         Author      Version     Description       tag
 * ------------------------------------------------------------------
 * 2022-08-19      jiangqi      1.0         1.0 Version
 * 2022-08-19      mazheng      1.0         1.0 Version      rev
 *
 */
@RestController
@Api("用户模块")
@ApiOperation("UserController")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户登录功能
     *  登录成功后存储session中
     * @param user          验证 登录输入项： 用户名，密码
     * @param request       请求对象
     * @return  登录结果
     */
    @ApiOperation(value = "登录")
    @GetMapping("/login")
    public Result<User> login(@Valid User user, HttpServletRequest request) {
        // 根据
        User userList = userService.login(user);

        if (userList == null) { // 用户名密码检索不到时
            return Result.error(CodeMsg.PWD_ERROR);
        } else { // 登录成功时处理
            // session中存储user信息
            request.getSession().setAttribute("user", userList);
            return Result.success(userList);
        }

    }
}
